import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
Vue.use(VueRouter)
const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/about/:id',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue'),
    meta:{isLogin:true},  // 配置路由守卫标识   标识当前的页面需要登陆权限
  },
 
  {
    path:"/login",
    name:"Login",
    component:()=>import("../views/Login.vue")
  },
  {
    path:"/my",
    name:"My",
    component:()=>import("../views/My.vue")
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
//添加全局前置守卫函数
router.beforeEach((to,from,next)=>{
  //to即将进入的路由
  //from即将离开的路由
  //next 管道中往下执行的函数
  if(to.meta.isLogin){
    if(localStorage.getItem("token")){//登录状态中
        //往下执行
        next();
    }
    else{
        next("/login")
    }
  }
  next()//直接执行
})

export default router
